#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

using namespace std;

typedef void (*work_t)();
typedef void (*task_t)();

void Task1()
{
    cout << "this is working task1 now!" << endl;
}

void Task2()
{
    cout << "this is working task2 now!" << endl;
}

void Task3()
{
    cout << "this is working task3 now!" << endl;
}

uint32_t NextTask()
{
    return rand() % 3;
}

task_t task[3] = {Task1, Task2, Task3};

void Worker()
{
    while (true)
    {
        uint32_t command_code = 0;
        //从标准输入中读取任务码，根据任务码
        ssize_t n = read(0, &command_code, sizeof(command_code));
        if(n == sizeof(command_code))
        {
            if(command_code >= 3) continue;
            task[command_code]();
            cout  << "I am worker: " << getpid() << endl << endl;
            sleep(1);
        }
        else if(n == 0)
        {
            cout << "sub process: " << getpid() << ", quit now..." << endl;
            break;
        }
    }
}